import { RouteConfig } from "vue-router";
// import Layout from "@/layout/index.vue";
import { ChildAppRoute, RouteMeta } from "../routeMeta";
import apiPortRoutes from "./apiPort";
import { addRoutesConfig } from "..";

const micAppRoutes: RouteConfig[] = [
  {
    path: "/micApp",
    // component: Layout,
    component: () => import("@/layout/back/index.vue"),
    redirect: "/micApp/child/app1",
    meta: new RouteMeta({
      title: "微前端",
      menuKey: "micApp"
    }),
    children: [
      {
        path: "child/*",
        component: () => import("@/qiankun/components/childapp.vue"),
        meta: new RouteMeta({
          title: "子应用",
          menuKey: "micApp-child",
          parentKeys: ["micApp"],
          defaultPath: "/micApp/child/app1"
        })
      },
      {
        path: "parent/*",
        component: () => import("@/qiankun/components/childapp.vue"),
        meta: new RouteMeta({
          title: "父应用",
          menuKey: "micApp-parent",
          parentKeys: ["micApp"],
          defaultPath: "/micApp/parent/app1"
        })
      },
      ...apiPortRoutes
    ]
  }
];

export function addChildAppRoutes(menuKey: string, routes: ChildAppRoute[]) {
  // 动态添加子应用菜单路由
  let micAppRoute = micAppRoutes[0];
  let actRoute = micAppRoute.children?.find(
    item => item.meta.menuKey === menuKey
  );
  if (actRoute) {
    let actMeta = actRoute.meta as RouteMeta;
    actMeta.childAppRoutes = routes;
    console.log("微前端路由",micAppRoutes)
    addRoutesConfig(micAppRoutes);
  } else {
    //   throw new Error("没有该路由")
    console.log("没有该路由");
  }
}

export function addApiPortMicAppRoutes(routes: ChildAppRoute[]) {
  // 添加api网关子应用路由
  addChildAppRoutes("apiPort", routes);
}

export default micAppRoutes;
